package cn.hicard.common;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.hicard.util.AppConfig;

public class WebSiteMap {
	
	static SiteMap siteMap;
	
	// 加载网站地图文件
	static{
		StringBuffer xml = new StringBuffer();
		InputStream is = AppConfig.class.getResourceAsStream("/webSiteMap.xml");
		BufferedReader reader = null;
		String line = null;
		try {
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			while((line = reader.readLine()) != null){
				xml.append(line);
			}
			Document doc = DocumentHelper.parseText(xml.toString());
			Element root = doc.getRootElement();
			siteMap = new SiteMap();
			siteMap.setTitle(root.attributeValue("title"));
			siteMap.setUrl(root.attributeValue("url"));
			
			initXML(root, siteMap);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (Exception e2) {
			}
		}
	}
	
	/** 初始化网站地导航XML */
	public static void initXML(Element root, SiteMap parent){
		@SuppressWarnings("unchecked")
		List<Element> list = root.elements();
		
		for (Element lmt : list) {
			SiteMap child = new SiteMap();
			child.setParent(parent);
			child.setTitle(lmt.attributeValue("title"));
			child.setUrl(lmt.attributeValue("url"));
			parent.addChild(child);
			if(lmt.elements().size()>0){
				initXML(lmt, child);
			}
		}	
	}
	
	/** 生成导航URL */
	public static String navigateURL(String rootUrl, String url){
		if(url == null || url.trim().isEmpty()){
			return siteMap.getTitle();
		}
		url = "~"+url;
		StringBuilder sb = new StringBuilder();
		SiteMap child = siteMap.findChild(url);
		if(child != null){
			// 当前层不要链接，直接显示
			String link = child.getTitle();
			// 每次将父级链接插入最前方
			sb.insert(0, link);
			while(child.getParent() != null){
				child = child.getParent();
				link = "<a href='"+child.getUrl().replace("~", rootUrl)+"'>"+ child.getTitle() + "</a>&gt;&gt;";
				sb.insert(0, link);
			}			
		}
		return sb.toString();
	}
	

}

/** 网站导航数据结构 */
class SiteMap{	
	
	private SiteMap parent;
	private List<SiteMap> children = new ArrayList<SiteMap>();
	private String title;
	private String url;
	
	public SiteMap getParent() {
		return parent;
	}
	public void setParent(SiteMap parent) {
		this.parent = parent;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
	/** 跟据指定URL查找节点，注意URL以根“/”开始 */
	public SiteMap findChild(String url){
		if(this.url.equals(url)){
			return this;
		}
		SiteMap child = null;	
		if(children != null && children.size()>0){
			for (SiteMap sm : children) {
				child = sm.findChild(url);
				if(child != null) break;
			}
		}
		return child;
	}
	/** 添加子 */
	public void addChild(SiteMap sm){
		children.add(sm);
	}
}
